﻿#include "stdafx.h"
#include "DrawCurve.h"
#include "MGData.h"

CDrawCurve::CDrawCurve()
{
	m_fYGap = 0;
	m_fXGap = 0;
	m_fYMax = 0;
	m_fYMin = 0;
	m_fXMin = 0;

	m_rtPic = 0;
	m_nRedNo = -1;
}


CDrawCurve::~CDrawCurve()
{
}

int CDrawCurve::GetLineType()
{
	int nLineStyle = PS_DOT;
	if (g_CoordSet.chLineType)
	{
		nLineStyle = PS_SOLID;
	}
	return nLineStyle;
}

vector<CRect> CDrawCurve::GetCircleRect()
{
	return m_vrtClrcle;
}

void CDrawCurve::GetData(DATA_TYPE Data_Type, CURVE_TYPE Curve_Type)
{
	if (Data_Type == DATA_0)
	{
		m_vQs.clear();
		QS_DATA qsData;
		for (int i = 0; i < (int)CMGData::GetInstance()->m_vDataInfo.size(); ++i)
		{
			qsData.fLali = CMGData::GetInstance()->m_vDataInfo[i].fCurLoad;	//拉力
			qsData.fWeiyi = CMGData::GetInstance()->m_vDataInfo[i].fWeiyiAvg;	//位移
			m_vQs.push_back(qsData);
		}
	}
	else
	{
		if (Curve_Type == CU_Q_s)
		{
			m_vMgQS = CMGData::GetInstance()->GetMgSummaryData();
		}
		else
		{
			GetMgSeQSpData(m_vMgSeQSp, CMGData::GetInstance()->GetMgSummaryData());
		}
	}
}


void CDrawCurve::GetMgSeQSpData(vector<MG_SEQSP> &vData, vector<TABLE_SUMMARY> vSumData)
{
	vData.clear();
	if (vSumData.empty())
	{
		return;
	}
	float fbaseVal = 0;			//初始位移
	float fMaxVal = 0;			//最大拉力位移
	float fLastVal = 0;			//最后位移值
	float fMaxLoad = 0;			//最大加载拉力
	BOOL bExistLoad = TRUE; //是否存在加载数据

	MG_SEQSP mg;
	mg.fLoadVal = vSumData[0].fLoadVal;
	mg.fSeVal = vSumData[0].fAllDisp;
	mg.fSpVal = vSumData[0].fAllDisp;
	vData.push_back(mg);

	int nBenji = vSumData[0].nBenJiId;
	int nJiaZai = vSumData[0].nJiaZaiOrXieZai;
	int nBegin = 0;
	for (int i = nBegin; i < (int)vSumData.size(); ++i)
	{
		//拉脱点
		mg.nLatuodian = vSumData[i].nLatuodian;
		if (i == nBegin)
		{
			fbaseVal = vSumData[i].fAllDisp;
		}

		if (nBenji != vSumData[i].nBenJiId)
		{
			if (i > nBegin && bExistLoad)
			{
				mg.fLoadVal = fMaxLoad;
				mg.fSeVal = fMaxVal - fLastVal;
				mg.fSpVal = fLastVal - fbaseVal;
				vData.push_back(mg);
			}

			fMaxLoad = vSumData[i].fLoadVal;
			fMaxVal = vSumData[i].fAllDisp;
			fLastVal = vSumData[i].fAllDisp;

			nBenji = vSumData[i].nBenJiId;
			nJiaZai = vSumData[i].nJiaZaiOrXieZai;
			if (nJiaZai == LOAD_VALUE)
			{
				bExistLoad = TRUE;
			}
			else
			{
				bExistLoad = FALSE;
			}

			if (i == vSumData.size() - 1)
			{
				if (bExistLoad)
				{
					mg.fLoadVal = fMaxLoad;
					mg.fSeVal = fMaxVal - fLastVal;
					mg.fSpVal = fLastVal - fbaseVal;
					vData.push_back(mg);
				}
			}
		}
		else
		{
			fLastVal = vSumData[i].fAllDisp;

			if (vSumData[i].nJiaZaiOrXieZai == LOAD_VALUE)
			{
				if (fMaxLoad <= vSumData[i].fLoadVal)
				{
					fMaxLoad = vSumData[i].fLoadVal;
					fMaxVal = vSumData[i].fAllDisp;
				}
			}

			if (i == vSumData.size() - 1)
			{
				if (bExistLoad)
				{
					mg.fLoadVal = fMaxLoad;
					mg.fSeVal = fMaxVal - fLastVal;
					mg.fSpVal = fLastVal - fbaseVal;
					vData.push_back(mg);
				}
			}
		}
	}
}

//获取QS数据横纵坐标最大值
void CDrawCurve::GetQSMaxVal(float &fHMax, float &fHMin, float &fVMax)
{
	fHMax = -10000;
	fVMax = -10000;
	fHMin = 0;
	for (int i = 0; i < (int)m_vQs.size(); ++i)
	{
		if (fHMax < m_vQs[i].fWeiyi)
		{
			fHMax = m_vQs[i].fWeiyi;
		}
		if (fVMax < m_vQs[i].fLali)
		{
			fVMax = m_vQs[i].fLali;
		}
		if (fHMin > m_vQs[i].fWeiyi)
		{
			fHMin = m_vQs[i].fWeiyi;
		}
	}
	fVMax = float(int(fVMax + 0.9));
	SetHVal(fHMax, fHMin);
}
//获取QS纵坐标最大值和最小值
void CDrawCurve::GetQSYMaxMinVal(float &fMax, float &fMin)
{
	fMax = -10000;
	fMin = 10000;
	for (int i = 0; i < (int)m_vQs.size(); ++i)
	{
		if (fMax < m_vQs[i].fLali)
		{
			fMax = m_vQs[i].fLali;
		}
		if (fMin > m_vQs[i].fLali)
		{
			fMin = m_vQs[i].fLali;
		}
	}
	if (g_CoordSet.chUserSet)
	{
		 fMax = g_CoordSet.fYMax;
		 fMin = g_CoordSet.fYMin;
	}
	else
	{
		if (fMax < 1)
		{
			fMax = 1;
		}
		else
		{
			fMax = float(int(fMax + 9.9));
		}
//		fMax = float(int(fMax + 0.9));
		fMin = float(int(fMin - 0.99));
		if (fMin > 0)
		{
			fMin = 0;
		}
	}
}


//获取MgQS数据横纵坐标最大值最小值
void CDrawCurve::GetMgQSMaxVal(float &fHMax, float &fHMin, float &fVMax)
{
	fHMax = -10000;
	fHMin = 0;
	fVMax = -10000;
	for (int i = 0; i < (int)m_vMgQS.size(); ++i)
	{
		if (fHMax < m_vMgQS[i].fAllDisp)
		{
			fHMax = m_vMgQS[i].fAllDisp;
		}
		if (fHMin > m_vMgQS[i].fAllDisp)
		{
			fHMin = m_vMgQS[i].fAllDisp;
		}

		if (fVMax < m_vMgQS[i].fLoadVal)
		{
			fVMax = m_vMgQS[i].fLoadVal;
		}
	}
	SetHVal(fHMax, fHMin);
}

//获取m_vMgSeQSp数据横纵坐标最大值最小值
void CDrawCurve::GetMgSqQSpMaxVal(float &fHMax, float &fHMin, float &fVMax)
{
	fHMax = -10000;
	fHMin = 0;
	fVMax = -10000;
	for (int i = 0; i < (int)m_vMgSeQSp.size(); ++i)
	{
		if (fHMax < m_vMgSeQSp[i].fSeVal)
		{
			fHMax = m_vMgSeQSp[i].fSeVal;
		}
		if (fHMax < m_vMgSeQSp[i].fSpVal)
		{
			fHMax = m_vMgSeQSp[i].fSpVal;
		}

		if (fHMin > m_vMgSeQSp[i].fSeVal)
		{
			fHMin = m_vMgSeQSp[i].fSeVal;
		}
		if (fHMin > m_vMgSeQSp[i].fSpVal)
		{
			fHMin = m_vMgSeQSp[i].fSpVal;
		}

		if (fVMax < m_vMgSeQSp[i].fLoadVal)
		{
			fVMax = m_vMgSeQSp[i].fLoadVal;
		}
	}
	fHMax = fHMax > abs(fHMin) ? fHMax : abs(fHMin);

	SetHVal(fHMax, fHMin);
}

//水平最大最小值限制
void CDrawCurve::SetHVal(float &fHMax, float &fHMin)
{
	if (fHMax <= 0)
	{
		fHMax = 0;
	}
	else if (fHMax <= 1)
	{
		fHMax = 1;
	}
	else if (fHMax < 20)
	{
		fHMax = float(int(fHMax + 0.99));
	}
	else if (fHMax < 100)
	{
		fHMax = float((int(fHMax / 5)) * 5 + 5);
	}
	else
	{
		fHMax = float((int(fHMax / 10)) * 10 + 10);
	}

	if (fHMin >= 0)
	{
		fHMin = 0;
	}
	else if (fHMin >= -1)
	{
		fHMin = -1;
	}
	else if (fHMin >= -20)
	{
		fHMin = float((int(fHMin / 2)) * 2 - 2);
	}
	else if (fHMin >= -100)
	{
		fHMin = float((int(fHMin / 5)) * 5 - 5);
	}
	else
	{
		fHMin = float((int(fHMin / 10)) * 10 - 10);
	}
}

//获取MgQS纵坐标最大值和最小值
vector<float> CDrawCurve::GetMgQSYMaxMinVal(float &fMax, float &fMin)
{
	fMax = -100000;	//纵坐标是kN
	fMin = 0;	//MgQs的最小值只能是0
	vector<float> vLoadValue;
	for (int i = 0; i < (int)m_vMgQS.size(); ++i)
	{
		if (fMax < m_vMgQS[i].fLoadVal)
		{
			fMax = m_vMgQS[i].fLoadVal;
		}
		if (fMin > m_vMgQS[i].fLoadVal)
		{
			fMin = m_vMgQS[i].fLoadVal;
		}
		BOOL bIn = FALSE;
		for (int v = 0; v < (int)vLoadValue.size() - 1; ++v)
		{
			if (m_vMgQS[i].fLoadVal == vLoadValue[v])
			{
				bIn = TRUE;
				break;
			}
		}
		if (!bIn)
		{
			vLoadValue.push_back(m_vMgQS[i].fLoadVal);
		}
	}
	if (g_CoordSet.chUserSet)
	{
		fMax = g_CoordSet.fYMax;
		fMin = g_CoordSet.fYMin;
	}
	else
	{
		fMax *= 1.1f;
	}

	return vLoadValue;
}

//画纵坐标
void CDrawCurve::DrawV(DATA_TYPE Data_Type, CURVE_TYPE Curve_Type, CDC *memdc, CRect rtCurve, CRect &rtPic, float &fYGap, float &fMin, BOOL bPrint)
{
	if (!g_bInitData)
	{
		return;
	}
	CPen pen, *OldPen = NULL;
	CBrush brush, *OldBrush = NULL;
	CString strVal(L""), strYUnit(L"");
	float fMax = 0;		//纵坐标最大值
	vector<float> vMgLoadVal; //锚杆纵坐标拉力值
	//获取纵坐标最大最小值
	if (Data_Type == DATA_0)
	{
		GetQSYMaxMinVal(fMax, fMin);
		if (m_vQs.empty())
		{
			fMax = 1;
			fMin = 0;
		}
		m_fYMax = fMax;
		m_fYMin = fMin;
		strYUnit = g_strCurveUnitY[0];
	}
	else 
	{
		vMgLoadVal = GetMgQSYMaxMinVal(fMax, fMin);
		if (m_vMgQS.empty())
		{
			fMax = 1;
			fMin = 0;
		}
		m_fYMax = fMax;
		m_fYMin = fMin;
		strYUnit = g_strCurveUnitY[0];
	}

	//获取位移长度最长的值
	strVal.Format(L"%.1f", fMax);
	CSize szFont;
	szFont = memdc->GetTextExtent(strVal);
	int nLenMax = 0;
	nLenMax = szFont.cx;
	strVal.Format(L"%.1f", fMin);
	szFont = memdc->GetTextExtent(strVal);
	nLenMax = nLenMax < szFont.cx ? szFont.cx : nLenMax;
	int nW = nLenMax + 15;
	int nH = 10;
	if (bPrint)
	{
		nW = nLenMax + FONT_TOP;
		nH = FONT_TOP;
	}

	int nToLeft = rtCurve.left + nW;	//画图左侧
	int nToTop = rtCurve.top + nH;		//画图上方
	//画图的宽度和高度
	int nWidth = rtCurve.Width() - 2 * nW;			//画图区域宽度
	int nHight = rtCurve.Height() - 2 * nH;			//画图区域高度

	//刻度高度
	int nTitleHig = KEDU;
	int nKeduHig = KEDU;	//长度
	if (bPrint)
	{
		nTitleHig = KEDU_PRINT;
		nKeduHig = KEDU_PRINT;	//长度
	}
	//向上
	nHight -= (szFont.cy + nKeduHig * 2);

	//画坐标轴
	brush.CreateSolidBrush(RGB(0, 0, 0));
	memdc->SelectObject(&brush);
	//画横坐标
	CPoint pt = 0;
	pt.x = nToLeft;
	pt.y = nToTop + nHight;

	memdc->MoveTo(pt);
	pt.x = nToLeft + nWidth;
	memdc->LineTo(pt);

	int nL = ARRAW_LEN;
	if (bPrint)
	{
		nL *= 7;
	}
	//箭头
	memdc->MoveTo(pt);
	memdc->LineTo(pt.x - nL, pt.y + nL / 2);
	memdc->MoveTo(pt);
	memdc->LineTo(pt.x - nL, pt.y - nL / 2);
	if (Curve_Type == CU_Se_Q_Sp) //左右双箭头
	{
		pt.x = nToLeft;
		memdc->MoveTo(pt);
		memdc->LineTo(pt.x + nL, pt.y + nL / 2);
		memdc->MoveTo(pt);
		memdc->LineTo(pt.x + nL, pt.y - nL / 2);
	}
	//竖坐标
	pt.x = nToLeft;
	if (Curve_Type == CU_Se_Q_Sp) //总坐标在中心轴
	{
		pt.x = nToLeft + nWidth / 2;
	}
	pt.y = nToTop + nHight;
	memdc->MoveTo(pt.x, pt.y);
	pt.y = nToTop;
	memdc->LineTo(pt.x, pt.y);
	//箭头
	memdc->MoveTo(pt.x, nToTop);
	memdc->LineTo(pt.x - nL / 2, nToTop + nL);
	memdc->MoveTo(pt.x, nToTop);
	memdc->LineTo(pt.x + nL / 2, nToTop + nL);
	//坐标轴 标签
	strVal = strYUnit;
	szFont = memdc->GetTextExtent(strVal);
	nL = 8;
	if (bPrint)
	{
		nL *= 7;
	}
	memdc->TextOutW(pt.x + nL, nToTop, strVal);
	memdc->SelectObject(OldBrush);
	brush.DeleteObject();

	int nToBottom = szFont.cy * 2;
	float fCha = (fMax - fMin);
	if (fCha > -EPSINON && fCha < EPSINON)
	{
		fYGap = 0;
	}
	else
	{
		fYGap = (nHight - nToBottom) / (fMax - fMin);
	}

	if (!bPrint)
	{
		m_fYGap = fYGap;
	}

	rtPic.left = nToLeft;
	rtPic.top = nToTop;
	rtPic.right = rtPic.left + nWidth;
	rtPic.bottom = rtPic.top + nHight;

	// 画纵坐标虚线
	float fGap = float(abs(fMax - fMin) / 10.0);
	int nEnd = 11;

	if (Data_Type != DATA_0)
	{
		if (vMgLoadVal.empty() || vMgLoadVal[0] != fMin)
		{
			vMgLoadVal.insert(vMgLoadVal.begin(), fMin);
		}
		nEnd = (int)vMgLoadVal.size();
	}
	for (int i = 0; i < nEnd; ++i)
	{
		//画刻度
		pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
		OldPen = memdc->SelectObject(&pen);
		pt.x = rtPic.left;
		if (Curve_Type == CU_Se_Q_Sp) //总坐标在中心轴
		{
			pt.x = rtPic.left + nWidth / 2;
		}
		if (Data_Type != DATA_0)
		{
			if (0 == i)
			{
				pt.y = int(rtPic.bottom);
			}
			else
			{
				pt.y = int(rtPic.bottom - fYGap * (vMgLoadVal[i] - fMin));
			}			
		}
		else
		{
			pt.y = int(rtPic.bottom - fYGap * fGap * i);
		}
		
		pt.x = pt.x - nKeduHig;
		memdc->MoveTo(pt);
		if (Curve_Type == CU_Se_Q_Sp)
		{
			memdc->LineTo(pt.x + nKeduHig * 2 + 1, pt.y);
		}
		else
		{
			memdc->LineTo(pt.x + nKeduHig, pt.y);
		}		
		
		//写刻度值
		if (i == 0)
		{
			strVal.Format(L"%.1f", fMin);
		}
		else
		{
			if (Data_Type != DATA_0)
			{
				strVal.Format(L"%.1f", vMgLoadVal[i]);
			}
			else
			{
				strVal.Format(L"%.1f", fGap * i + fMin);
			}
		}
		szFont = memdc->GetTextExtent(strVal);
		nL = 2;
		if (bPrint)
		{
			nL = 20;
		}
		memdc->TextOutW(pt.x - szFont.cx - nL, pt.y - szFont.cy / 2, strVal);

		memdc->SelectObject(OldPen);
		pen.DeleteObject();

		//画辅助虚线
		if (i > 0 && g_CoordSet.bCurveShow)
		{
			pen.CreatePen(PS_DOT, 1, RGB(200, 200, 200));
			OldPen = memdc->SelectObject(&pen);
			pt.x = nToLeft;
			memdc->MoveTo(pt);
			pt.x = nToLeft + nWidth;
			memdc->LineTo(pt);
			memdc->SelectObject(OldPen);
			pen.DeleteObject();
		}
	}
	if (!bPrint)
	{
		m_rtPic = rtPic;
	}
}

//画横坐标刻度 返回刻度区域
CRect CDrawCurve::DrawKeduVal(CDC *memdc, CRect rtPic, float fXGap, float fVal, int nKedu)
{
	CRect rt = 0;
	CPoint pt;
	CString strVal(L"");
	CSize szFont = 0;
	pt.x = int(rtPic.left + fXGap * fVal);
	pt.y = rtPic.bottom;
	memdc->MoveTo(pt);
	pt.y += nKedu;
	memdc->LineTo(pt);

	strVal.Format(L"%.1f", fVal);
	szFont = memdc->GetTextExtent(strVal);
	rt.left = pt.x - szFont.cx / 2;
	rt.right = pt.x + szFont.cx / 2;
	rt.top = pt.y + 2;
	return rt;
}

void CDrawCurve::DrawCurve_Qs(CDC *memdc, CRect rtCurve, BOOL bPrint)
{
	if (!g_bInitData)
	{
		return;
	}
	//获取数据
	GetData(DATA_0, CU_Q_s);
	if (m_vQs.empty())
	{
		return;
	}
	memdc->FillSolidRect(rtCurve, COLOR_WHITE);
	CFont font, *OldFont = NULL;
	if (!bPrint)
	{
		//获取字体大小
		SetWindowFont(font);
		OldFont = memdc->SelectObject(&font);
	}
	CPen pen, *OldPen = NULL;
	CBrush brush, *OldBrush = NULL;
	CRect rtPic = 0;
	float fYGap = 0;
	float fYMin = 0;
	CSize szFont;
	CString strVal(L"");
	CPoint pt = 0;
	int nL = 10;
	//画标题
	if (g_CoordSet.chTitleShow)
	{
		strVal = g_strCurveTitle[0];
		szFont = memdc->GetTextExtent(strVal);
		if (bPrint)
		{
			nL = FONT_TOP;
		}
		memdc->TextOutW(rtCurve.left + rtCurve.Width() / 2 - szFont.cx / 2, rtCurve.top + nL, strVal);

		rtCurve.top += nL + szFont.cy;
	}

	//画纵坐标
	DrawV(DATA_0, CU_Q_s, memdc, rtCurve, rtPic, fYGap, fYMin, bPrint);
	int nKeduHig = KEDU;
	if (bPrint)
	{
		nKeduHig = KEDU_PRINT;
		m_vrtClrcle.clear();
	}
	strVal = g_strCurveUnitX[0];
	szFont = memdc->GetTextExtent(g_strCurveUnitX[0]);
	int nToRight = szFont.cx * 2;	//右侧
	rtPic.right -= nToRight;
	szFont = memdc->GetTextExtent(strVal);
	memdc->TextOutW(rtCurve.right - szFont.cx - 5, rtPic.bottom + 3, strVal);

	//横坐标
	float fHMax = 0;		//横坐标最大值
	float fHMin = 0;		//横坐标最小值
	float fVMax = 0;		//纵坐标最大值
	if (g_CoordSet.chUserSet) //用户自定义设定
	{
		fHMax = g_CoordSet.fXMax;
		fVMax = g_CoordSet.fYMax;
		fHMin = g_CoordSet.fXMin;
	}
	else
	{
		GetQSMaxVal(fHMax, fHMin, fVMax);
	}

	float fXGap = 0;	//x轴间距 
	if (fHMax - fHMin >= -EPSINON && fHMax - fHMin <= EPSINON)
	{
		fXGap = 0;
	}
	else
	{
		fXGap = (rtPic.Width()) / (fHMax - fHMin);
	}
	if (!bPrint)
	{
		m_fXGap = fXGap;
	}

	//画横坐标
	pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	OldPen = memdc->SelectObject(&pen);
	CRect rtCur = 0;
	float fGap = 0;
	float fVal = 0;
	fGap = float((fHMax - fHMin) / 10.0);
	for (int i = 0; i < 11; ++i)
	{
		fVal = i * fGap + fHMin;
		rtCur = DrawKeduVal(memdc, rtPic, fXGap, fVal - fHMin, nKeduHig);
		strVal.Format(L"%.1f", fVal);
		memdc->TextOutW(rtCur.left, rtCur.top, strVal);
	}
	memdc->SelectObject(OldPen);
	pen.DeleteObject();

	if (!bPrint)
	{
		m_fXMin = fHMin;
		m_vrtClrcle.clear();
	}

	//画所有数值的点
	CRect rtCircle = 0;
	vector<CRect> vRt;
	for (int i = 0; i < (int)m_vQs.size(); ++i)
	{
		pt.x = int(rtPic.left + fXGap * (m_vQs[i].fWeiyi - fHMin));
		pt.y = int(rtPic.bottom - fYGap * (m_vQs[i].fLali - fYMin));

		if (i == 0)
		{
			if (m_vQs.size() != 1)
			{
				memdc->MoveTo(pt);
			}
		}
		else
		{
			//虚线
			pen.CreatePen(GetLineType(), 1, g_CoordSet.clrLoad);
			OldPen = memdc->SelectObject(&pen);

			memdc->LineTo(pt);

			memdc->SelectObject(OldPen);
			pen.DeleteObject();
		}

		if (m_nRedNo == i)
		{
			nL = CIRCLE_RED_W;
			if (bPrint)
			{
				nL = CIRCLE_RED_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		else
		{
			nL = CIRCLE_W;
			if (bPrint)
			{
				nL = CIRCLE_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		vRt.push_back(rtCircle);
	}
	if (!bPrint)
	{
		m_vrtClrcle = vRt;
	}
	//画圆
	for (int i = 0; i < (int)vRt.size(); ++i)
	{
		if (m_nRedNo == i)
		{
			brush.CreateSolidBrush(COLOR_RED);
		}
		else
		{
			brush.CreateSolidBrush(g_CoordSet.clrSample);
		}
		OldBrush = memdc->SelectObject(&brush);
		//画圆
		memdc->Ellipse(vRt[i]);
		memdc->SelectObject(OldBrush);
		brush.DeleteObject();
	}
	if (!bPrint)
	{
		memdc->SelectObject(OldFont);
		font.DeleteObject();
	}
}

void CDrawCurve::DrawCurve_MgQs(CDC *memdc, CRect rtCurve, BOOL bPrint)
{
	if (!g_bInitData)
	{
		return;
	}
	GetData(DATA_2, CU_Q_s);
	if (m_vMgQS.empty())
	{
		return;
	}
	MG_PROJECT_INFO	Pro_Info = CMGData::GetInstance()->m_MGProInfo;
	memdc->FillSolidRect(rtCurve, COLOR_WHITE);
	if (CMGData::GetInstance()->m_vDataInfo.empty())
	{
		return;
	}
	CFont font, *OldFont = NULL;
	if (!bPrint)
	{
		//获取字体大小
		SetWindowFont(font);
		OldFont = memdc->SelectObject(&font);
	}

	CPen pen, *OldPen = NULL;
	CBrush brush, *OldBrush = NULL;
	CRect rtPic = 0;
	float fYGap = 0;
	float fYMin = 0;
	CSize szFont;
	CString strVal(L"");
	CPoint pt = 0;

	int nL = 10;
	//画标题
	if (g_CoordSet.chTitleShow)
	{
		strVal = g_strCurveTitle[0];
		szFont = memdc->GetTextExtent(strVal);
		if (bPrint)
		{
			nL = FONT_TOP;
		}
		memdc->TextOutW(rtCurve.left + rtCurve.Width() / 2 - szFont.cx / 2, rtCurve.top + nL, strVal);

		rtCurve.top += nL + szFont.cy;
	}

	//画纵坐标
	DrawV(DATA_2, CU_Q_s, memdc, rtCurve, rtPic, fYGap, fYMin, bPrint);

	int nKeduHig = KEDU;
	if (bPrint)
	{
		nKeduHig = KEDU_PRINT;
	}
	//
	strVal = g_strCurveUnitX[0];
	szFont = memdc->GetTextExtent(g_strCurveUnitX[0]);
	int nToRight = szFont.cx * 2;	//右侧
	rtPic.right -= nToRight;
	szFont = memdc->GetTextExtent(strVal);
	memdc->TextOutW(rtCurve.right - szFont.cx - 5, rtPic.bottom + 3, strVal);

	float fHMax = 0;		//横坐标最大值
	float fHMin = 0;		//横坐标最小值
	float fVMax = 0;		//纵坐标最大值
	if (g_CoordSet.chUserSet) //用户自定义设定
	{
		fHMax = g_CoordSet.fXMax;
		fVMax = g_CoordSet.fYMax;
		fHMin = g_CoordSet.fXMin;
	}
	else
	{
		GetMgQSMaxVal(fHMax, fHMin, fVMax);
	}

	float fXGap = 0;	//x轴间距 
	if ((fHMax - fHMin) >= -EPSINON && (fHMax - fHMin) <= EPSINON)
	{
		fXGap = 0;
	}
	else
	{
		fXGap = (rtPic.Width()) / (fHMax - fHMin);
	}
	if (!bPrint)
	{
		m_fXGap = fXGap;
	}

	//画横坐标
	pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	OldPen = memdc->SelectObject(&pen);
	CRect rtCur = 0;
	float fGap = 0;
	float fVal = 0;
	fGap = float((fHMax - fHMin) / 10.0);
	for (int i = 0; i < 11; ++i)
	{
		fVal = i * fGap + fHMin;
		rtCur = DrawKeduVal(memdc, rtPic, fXGap, fVal - fHMin, nKeduHig);
		strVal.Format(L"%.1f", fVal);
		memdc->TextOutW(rtCur.left, rtCur.top, strVal);
	}
	memdc->SelectObject(OldPen);
	pen.DeleteObject();


	if (!bPrint)
	{
		m_fXMin = fHMin;
		m_vrtClrcle.clear();
	}
	//画所有数值的点
	CRect rtCircle = 0;
	vector<CRect> vRt;
	for (int i = 0; i < (int)m_vMgQS.size(); ++i)
	{
		//拉脱点不绘制
		if (m_vMgQS[i].nLatuodian)
		{
			break;
		}
		pt.x = int(rtPic.left + fXGap * (m_vMgQS[i].fAllDisp - fHMin));
		pt.y = int(rtPic.bottom - fYGap * (m_vMgQS[i].fLoadVal - fYMin));

		if (i == 0)
		{
			if (m_vMgQS.size() != 1)
			{
				memdc->MoveTo(pt);
			}
		}
		else
		{
			//虚线
			if (m_vMgQS[i].nJiaZaiOrXieZai == LOAD_VALUE)
			{
				pen.CreatePen(GetLineType(), 1, g_CoordSet.clrLoad);
			}
			else
			{
				pen.CreatePen(GetLineType(), 1, g_CoordSet.clrLoadOff);
			}
			
			OldPen = memdc->SelectObject(&pen);

			memdc->LineTo(pt);

			memdc->SelectObject(OldPen);
			pen.DeleteObject();
		}

		if (m_nRedNo == i)
		{
			nL = CIRCLE_RED_W;
			if (bPrint)
			{
				nL = CIRCLE_RED_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		else
		{
			nL = CIRCLE_W;
			if (bPrint)
			{
				nL = CIRCLE_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		vRt.push_back(rtCircle);
	}
	if (!bPrint)
	{
		m_vrtClrcle = vRt;
	}
	//画圆
	for (int i = 0; i < (int)vRt.size(); ++i)
	{
		if (m_nRedNo == i)
		{
			brush.CreateSolidBrush(COLOR_RED);
		}
		else
		{
			brush.CreateSolidBrush(g_CoordSet.clrSample);
		}
		OldBrush = memdc->SelectObject(&brush);
		//画圆
		memdc->Ellipse(vRt[i]);
		memdc->SelectObject(OldBrush);
		brush.DeleteObject();
	}
	if (!bPrint)
	{
		memdc->SelectObject(OldFont);
		font.DeleteObject();
	}
}

//画SeQSp刻度
CRect CDrawCurve::DrawMgSeQSpKeduVal(CDC *memdc, CRect rtPic, float fXGap, float fVal, int nKedu, BOOL bLeft)
{
	CRect rt = 0;
	CPoint pt;
	CString strVal(L"");
	CSize szFont;
	pt.x = int(rtPic.left + fXGap * fVal);
	if (bLeft)
	{
		pt.x = int(rtPic.left + rtPic.Width() / 2 - fXGap * fVal);
	}
	else
	{
		pt.x = int(rtPic.left + rtPic.Width() / 2 + fXGap * fVal);
	}

	if (g_CoordSet.chUp)
	{
		pt.y = rtPic.bottom;
	}
	else
	{
		pt.y = rtPic.top;
	}
	memdc->MoveTo(pt);
	if (g_CoordSet.chUp)
	{
		pt.y += nKedu;
	}
	else
	{
		pt.y -= nKedu;
	}

	memdc->LineTo(pt);
	//写刻度值
	strVal.Format(L"%.1f", fVal);
	szFont = memdc->GetTextExtent(strVal);
	rt.left = pt.x - szFont.cx / 2;
	rt.right = pt.x + szFont.cx / 2;

	if (g_CoordSet.chUp)
	{
		rt.top = pt.y + 2;
	}
	else
	{
		rt.top = pt.y - szFont.cy;
	}
	return rt;
}

void CDrawCurve::DrawCurve_MgSeQSp(CDC *memdc, CRect rtCurve, BOOL bPrint)
{
	if (!g_bInitData)
	{
		return;
	}
	GetData(DATA_1, CU_Se_Q_Sp);
	if (m_vMgSeQSp.empty())
	{
		return;
	}
	MG_PROJECT_INFO	Pro_Info = CMGData::GetInstance()->m_MGProInfo;
	memdc->FillSolidRect(rtCurve, COLOR_WHITE);
	if (CMGData::GetInstance()->m_vDataInfo.empty())
	{
		return;
	}
	CFont font, *OldFont = NULL;
	if (!bPrint)
	{
		//获取字体大小
		SetWindowFont(font);
		OldFont = memdc->SelectObject(&font);
	}

	CPen pen, *OldPen = NULL;
	CBrush brush, *OldBrush = NULL;
	CRect rtPic = 0;
	float fYGap = 0;
	float fYMin = 0;
	CSize szFont;
	CString strVal(L"");
	CPoint pt = 0;

	int nL = 10;
	//画标题
	if (g_CoordSet.chTitleShow)
	{
		strVal = g_strCurveTitle[1];
		szFont = memdc->GetTextExtent(strVal);
		if (bPrint)
		{
			nL = FONT_TOP;
		}
		memdc->TextOutW(rtCurve.left + rtCurve.Width() / 2 - szFont.cx / 2, rtCurve.top + nL, strVal);

		rtCurve.top += nL + szFont.cy;
	}

	//画纵坐标
	DrawV(DATA_1, CU_Se_Q_Sp, memdc, rtCurve, rtPic, fYGap, fYMin, bPrint);

	int nKeduHig = KEDU;
	if (bPrint)
	{
		nKeduHig = KEDU_PRINT;
	}
	//
	strVal = g_strCurveUnitX[2];
	szFont = memdc->GetTextExtent(strVal);
	//左右各留两个空间
	int nToRight = szFont.cx;	//右侧
	rtPic.right -= nToRight;
	rtPic.left += nToRight;
	//写横坐标 标识
	szFont = memdc->GetTextExtent(strVal);
	if (g_CoordSet.chUp)
	{
		memdc->TextOutW(rtCurve.right - szFont.cx - 5, rtPic.bottom + 3, strVal);
	}
	else
	{
		memdc->TextOutW(rtCurve.right - szFont.cx - 5, rtPic.top - szFont.cy - 3, strVal);
	}
	strVal = g_strCurveUnitX[1];
	szFont = memdc->GetTextExtent(strVal);
	if (g_CoordSet.chUp)
	{
		memdc->TextOutW(rtCurve.left + 5, rtPic.bottom + 3, strVal);
	}
	else
	{
		memdc->TextOutW(rtCurve.left + 5, rtPic.top - szFont.cy - 3, strVal);
	}


	float fHMax = 0;		//横坐标最大值
	float fHMin = 0;		//横坐标最小值
	float fVMax = 0;		//纵坐标最大值

	GetMgSqQSpMaxVal(fHMax, fHMin, fVMax);
	fHMin = 0;
	if (g_CoordSet.chUserSet) //用户自定义设定
	{
		fHMax = g_CoordSet.fXMax;
		fVMax = g_CoordSet.fYMax;
	}

	float fXGap = 0;	//x轴间距 
	if ((fHMax - fHMin) >= -EPSINON && (fHMax - fHMin) <= EPSINON)
	{
		fXGap = 0;
	}
	else
	{
		fXGap = (rtPic.Width() / 2) / (fHMax - fHMin);
	}
	if (!bPrint)
	{
		m_fXGap = fXGap;
	}

	//画横坐标
	pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	OldPen = memdc->SelectObject(&pen);
	CRect rtCur = 0;
	float fGap = 0;
	float fVal = 0;
	fGap = float((fHMax - fHMin) / 5.0);
	for (int i = 0; i < 6; ++i)
	{
		fVal = i * fGap + fHMin;
		//右侧刻度
		strVal.Format(L"%.1f", fVal);
		rtCur = DrawMgSeQSpKeduVal(memdc, rtPic, fXGap, fVal - fHMin, nKeduHig, FALSE);
		memdc->TextOutW(rtCur.left, rtCur.top, strVal);
		//左侧刻度
		if (i > 0)
		{
			rtCur = DrawMgSeQSpKeduVal(memdc, rtPic, fXGap, fVal - fHMin, nKeduHig, TRUE);
			strVal.Format(L"-%.1f", fVal);
			memdc->TextOutW(rtCur.left, rtCur.top, strVal);
		}		
	}
	memdc->SelectObject(OldPen);
	pen.DeleteObject();


	if (!bPrint)
	{
		m_fXMin = fHMin;
		m_vrtClrcle.clear();
	}
	//画所有数值的点
	CRect rtCircle = 0;
	vector<CRect> vRt;
	int nMid = rtPic.left + rtPic.Width() / 2;

	//去掉第一个点从初始点开始绘图
	m_vMgSeQSp.erase(m_vMgSeQSp.begin());
	//先画左侧
	for (int i = 0; i < (int)m_vMgSeQSp.size(); ++i)
	{
		//拉脱点不绘制
		if (m_vMgSeQSp[i].nLatuodian)
		{
			break;
		}
		TRACE(L"塑性位移=%.1f\r\n", m_vMgSeQSp[i].fSeVal);
		pt.x = int(nMid - fXGap * (m_vMgSeQSp[i].fSeVal - fHMin));
		if (g_CoordSet.chUp)
		{
			pt.y = int(rtPic.bottom - fYGap * (m_vMgSeQSp[i].fLoadVal - fYMin));
		}
		else
		{
			pt.y = int(rtPic.top + fYGap * (m_vMgSeQSp[i].fLoadVal - fYMin));
		}

		if (i == 0)
		{
			if (m_vMgSeQSp.size() != 1)
			{
				memdc->MoveTo(pt);
			}
		}
		else
		{
			//虚线
			pen.CreatePen(GetLineType(), 1, g_CoordSet.clrLoad);
			OldPen = memdc->SelectObject(&pen);

			memdc->LineTo(pt);

			memdc->SelectObject(OldPen);
			pen.DeleteObject();
		}
		//没有选中
		if (FALSE && m_nRedNo == i)
		{
			nL = CIRCLE_RED_W;
			if (bPrint)
			{
				nL = CIRCLE_RED_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		else
		{
			nL = CIRCLE_W;
			if (bPrint)
			{
				nL = CIRCLE_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		vRt.push_back(rtCircle);
	}
	if (!bPrint)
	{
		//		m_vrtClrcle = vRt;
	}
	//画圆
	for (int i = 0; i < (int)vRt.size(); ++i)
	{
		if (m_nRedNo == i)
		{
			brush.CreateSolidBrush(COLOR_RED);
		}
		else
		{
			brush.CreateSolidBrush(g_CoordSet.clrSample);
		}
		OldBrush = memdc->SelectObject(&brush);
		//画圆
		memdc->Ellipse(vRt[i]);
		memdc->SelectObject(OldBrush);
		brush.DeleteObject();
	}
	//画右侧
	for (int i = 0; i < (int)m_vMgSeQSp.size(); ++i)
	{
		//拉脱点不绘制
		if (m_vMgSeQSp[i].nLatuodian)
		{
			break;
		}
		TRACE(L"塑性位移=%.1f\r\n", m_vMgSeQSp[i].fSpVal);
		pt.x = int(nMid + fXGap * (m_vMgSeQSp[i].fSpVal - fHMin));
		if (g_CoordSet.chUp)
		{
			pt.y = int(rtPic.bottom - fYGap * (m_vMgSeQSp[i].fLoadVal - fYMin));
		}
		else
		{
			pt.y = int(rtPic.top + fYGap * (m_vMgSeQSp[i].fLoadVal - fYMin));
		}

		if (i == 0)
		{
			if (m_vMgSeQSp.size() != 1)
			{
				memdc->MoveTo(pt);
			}
		}
		else
		{
			//虚线
			pen.CreatePen(GetLineType(), 1, g_CoordSet.clrLoad);
			OldPen = memdc->SelectObject(&pen);

			memdc->LineTo(pt);

			memdc->SelectObject(OldPen);
			pen.DeleteObject();
		}
		//没有选中
		if (FALSE && m_nRedNo == i)
		{
			nL = CIRCLE_RED_W;
			if (bPrint)
			{
				nL = CIRCLE_RED_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		else
		{
			nL = CIRCLE_W;
			if (bPrint)
			{
				nL = CIRCLE_W * 5;
			}
			rtCircle.left = pt.x - nL;
			rtCircle.top = pt.y - nL;
			rtCircle.right = pt.x + nL;
			rtCircle.bottom = pt.y + nL;
		}
		vRt.push_back(rtCircle);
	}
	if (!bPrint)
	{
		//		m_vrtClrcle = vRt;
	}
	//画圆
	for (int i = 0; i < (int)vRt.size(); ++i)
	{
		if (m_nRedNo == i)
		{
			brush.CreateSolidBrush(COLOR_RED);
		}
		else
		{
			brush.CreateSolidBrush(g_CoordSet.clrSample);
		}
		OldBrush = memdc->SelectObject(&brush);
		//画圆
		memdc->Ellipse(vRt[i]);
		memdc->SelectObject(OldBrush);
		brush.DeleteObject();
	}
	if (!bPrint)
	{
		memdc->SelectObject(OldFont);
		font.DeleteObject();
	}
}

//获取鼠标点的当前值
void CDrawCurve::GetMouseValue(DATA_TYPE DataType, CURVE_TYPE Curve_Type, CPoint pt, float &fXVal, float &fYVal)
{
	if (!g_bInitData)
	{
		return;
	}
	if (DataType == DATA_0 || Curve_Type == CU_Q_s)
	{
		if (m_fXGap >= -EPSINON && m_fXGap <= EPSINON)
		{
			fXVal = 0;
		}
		else
		{
			fXVal = (pt.x - m_rtPic.left) / m_fXGap;
		}
		fXVal += m_fXMin;

		if (m_fYGap >= -EPSINON && m_fYGap <= EPSINON)
		{
			fYVal = 0 + m_fYMin;
		}
		else
		{
			fYVal = (m_rtPic.bottom - pt.y) / m_fYGap + m_fYMin;
		}
	}
}

//获取YGap
float CDrawCurve::GetYGap(DATA_TYPE DataType, CURVE_TYPE Curve_Type)
{
	float fVal = m_fXGap;
	if (fVal >= -EPSINON && fVal <= EPSINON)
	{
		fVal = (float)0.001;
	}
	return fVal;
}

